package com.t.bricks.bussiness.db.dao.filecabinet.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.t.bricks.bussiness.db.dao.filecabinet.FileCabinetDao;
import com.t.bricks.bussiness.db.entity.filecabinet.FileCabinet;
import com.t.bricks.bussiness.db.mapper.filecabinet.FileCabinetMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.model.Page;

@Repository
public class FileCabinetDaoImpl implements FileCabinetDao {
	
	@Autowired
	private FileCabinetMapper fileCabinetMapper;
	
	/**
	 * 添加
	 * @param FileCabinet
	 * @return
	 */
	@Override
	public MsgEmity add(FileCabinet FileCabinet) {
		try {
			int i = fileCabinetMapper.add(FileCabinet);
			if (i < 1) {
				Log.error("新增未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "新增失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("新增时发生异常:", e.getMessage());
			return new MsgEmity(false, "新增失败！", 7002);
		}
		
		return new MsgEmity(true, "新增成功！", FileCabinet);
	}

	/**
	 * 删除
	 * @param sId
	 * @param iVersion
	 * @return
	 */
	@Override
	public MsgEmity del(String sId, Integer iVersion) {
		try {
			int i = fileCabinetMapper.del(sId, iVersion);
			if (i < 1) {
				Log.error("删除未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "删除请求失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("删除时发生异常:", e.getMessage());
			return new MsgEmity(false, "删除失败！", 7002);
		}
		
		return new MsgEmity(true, "删除成功！", 7009);
	}

	/**
	 * 修改
	 * 注意:更新成功后对象属性iVersion值将被+1
	 * @param fileCabinet
	 * @return
	 */
	@Override
	public MsgEmity edit(FileCabinet fileCabinet) {
		try {
			int i = fileCabinetMapper.edit(fileCabinet);
			if (i < 1) {
				Log.error("修改未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "修改失败！", 7201);
			}
		} catch (Exception e) {
			Log.error("修改操作发生异常:", e.getMessage());
			return new MsgEmity(false, "修改发生异常！", 7202);
		}
		
		return new MsgEmity(true, "修改成功！", 7009);
	}
	
	/**
	 * 根据Id查询数据
	 * @param sId
	 * @return
	 */
	@Override
	public MsgEmity findById(String sId) {
		FileCabinet FileCabinet = null;
		try {
			FileCabinet = fileCabinetMapper.findById(sId);
			if (null == FileCabinet) {
				return new MsgEmity(false, "文件信息数据不存在！", 7101);
			}
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "文件信息查询失败，发生异常！", 7103);
		}
		
		return new MsgEmity(true, "文件信息查询成功！", FileCabinet);
	}

	/**
	 * 根据关键值取对象
	 * @param FileCabinetData 存放查询条件的FileCabinet对象
	 * @return
	 */
	@Override
	public MsgEmity findByKey(FileCabinet FileCabinetData) {
		FileCabinet FileCabinet = null;
		try {
			FileCabinet = fileCabinetMapper.findByKey(FileCabinetData);
			if (null == FileCabinet) {
				return new MsgEmity(false, "数据不存在！", 7000);
			}
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7001);
		}
		
		return new MsgEmity(true, "查询成功！", FileCabinet);
	}

	/**
	 * 根据关键值查数量
	 * @param FileCabinetData 存放查询条件的FileCabinet对象
	 * @return
	 */
	@Override
	public int findCountByKey(FileCabinet FileCabinetData) {
		int iCount = 0;
		try {
			iCount = fileCabinetMapper.findCountByKey(FileCabinetData);
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return 0;
		}
		
		return iCount;
	}

	/**
	 * 检查待修改内容是否存在重复数据
	 * @param FileCabinetData
	 * @return 重复返回true
	 */
	@Override
	public boolean checkRepeatByEdit(FileCabinet FileCabinetData) {
		try {
			int i = fileCabinetMapper.checkRepeatByEdit(FileCabinetData);
			return i > 0;
		} catch (Exception e) {
			Log.error("检查待修改内容是否存在重复数据发生异常:", e.getMessage());
			return false;
		}
	}

	/**
	 * 检查待新增内容是否存在重复数据
	 * @param FileCabinetData
	 * @return 重复返回true
	 */
	@Override
	public boolean checkRepeatByAdd(FileCabinet FileCabinetData) {
		try {
			int i = fileCabinetMapper.checkRepeatByAdd(FileCabinetData);
			return i > 0;
		} catch (Exception e) {
			Log.error("检查待新增内容是否存在重复数据发生异常:", e.getMessage());
			return false;
		}
	}
	
	/**
	 * 查询分页信息
	 * @param map
	 * @param orders
	 * @param page
	 * @return
	 */
	@Override
	public MsgEmity findByPage(Map<String, Object> map, String orders, Page page) {
		int iCount = 0;
		int startRow = (page.getPageCurrent() -1) * page.getPageSize();
		List<FileCabinet> resultList = new ArrayList<FileCabinet>();
		page.setRecordList(resultList);

		String sName = (String)map.get("sName");
		String sProgram = (String)map.get("sProgram");
		Integer iType = (Integer)map.get("iType");
		String sMemo = (String)map.get("sMemo");
		try {
			iCount = fileCabinetMapper.findByPageCount(
					sName,
					sProgram,
					iType,
					sMemo);
		} catch (Exception e) {
			Log.error("查询数量发生异常", e.getMessage());
			return new MsgEmity(false, "查询失败！", page);
		}
		
		if (iCount < 1) {
			return new MsgEmity(true, "没有相关数据！", page);
		}

		page.setCountRow(iCount);

		List<FileCabinet> list = null;
		try {
			list = fileCabinetMapper.findByPage(
					sName,
					sProgram,
					iType,
					sMemo,
					orders, startRow, page.getPageSize());
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
			}
		} catch (Exception e) {
			Log.error("查询分页数据发生异常:", e.getMessage());
		}

		if (list == null) {
			return new MsgEmity(false, "查询失败！", page);
		}

		page.setRecordList(list);
		return new MsgEmity(true, "查询成功！", page);
	}
	
	/**
	 * 判断是否存在相同记录
	 * @param sName
	 * @param sProgram
	 * @param sFileMd5
	 * @return
	 */
	@Override
	public MsgEmity findSameDataCount(String sName, String sProgram, String sFileMd5) {
		try {
			int i = fileCabinetMapper.findSameDataCount(sName, sProgram, sFileMd5);
			if (0 == i) {
				return new MsgEmity(false, "查询成功！没发现重复", 7007);
			}
		} catch (Exception e) {
			Log.error("检查待修改内容是否存在重复数据发生异常:", e.getMessage());
			return MsgEmity.err(7008, "检查待修改内容是否存在重复数据发生异常：", e.getMessage());
		}
		
		return new MsgEmity(true, "查询成功！发现重复", 7009);
	}
	
	/**
	 * 根据项目名和文件key查询数据
	 * @param sProgram
	 * @param fileName
	 * @param sFileMd5
	 */
	@Override
	public MsgEmity findByNameAndKey(String sProgram, String fileName, String sFileMd5) {
		FileCabinet FileCabinet = null;
		try {
			FileCabinet = fileCabinetMapper.findByNameAndKey(sProgram, fileName, sFileMd5);
			if (null == FileCabinet) {
				return new MsgEmity(true, "文件信息记录为空！", 7001);
			}
		} catch (Exception e) {
			Log.error("查询文件信息记录发生异常:", e.getMessage());
			return new MsgEmity(false, "查询文件信息记录失败！", 7003);
		}
		
		return new MsgEmity(true, "查询成功！", FileCabinet);
	}
	
	/**
	 * 查找文件存储路径路径
	 * @param sId
	 * @return
	 */
	@Override
	public MsgEmity findPathById(String sId) {
		String sPath = null;
		try {
			sPath = fileCabinetMapper.findPathById(sId);
			if (null == sPath) {
				return new MsgEmity(true, "文件信息记录为空！", 7001);
			}
		} catch (Exception e) {
			Log.error("查询文件信息记录发生异常:", e.getMessage());
			return new MsgEmity(false, "查询文件信息记录失败！", 7003);
		}
		
		return new MsgEmity(true, "查询成功！", sPath);
	}
	
}
